/**
 * 适当性确认
 */
define(['ldfw/core', 'art', 'text!tpl/product/approp_confirm.tpl','text!tpl/product/approp_result.tpl',
        'text!tpl/product/risk_notification.tpl',
        'comps/ajax', 'comps/session', 'ldfw/ldlibs/dict','ldfw/utils/format', 'ldfw/helper/formatHelper'],
    function (LDFW, art, tpl, resultTpl, notificationTpl, ajax,session,dict) {
        var View = LDFW.Container.extend({
            className: 'panel',
            tpl: tpl,
            events:{
                'click [operate="continue"]':'continuePurchase',
                'click [operate="cancel"]':'cancelPurchase',
                'change #accept-confirm':'acceptConfirm'
            },
            initialize:function(){
                this.on('shown',this.loadData)
            },
            loadData:function(){
                const _this = this
                ajax.post('pfL.1.214',{pd_no:this.pd_no}).success(function(resp){
                    let productInfo = resp.result[0]
                    _this.productInfo = productInfo
                    _this.analysisAppropResult(productInfo)
                })
            },
            setProductNo:function(pd_no){
                this.pd_no = pd_no
            },
            continuePurchase:function(){
                const _this = this
                let user = session.getCurrentUser(),
                    productInfo = this.productInfo
                ajax.post('cpliL.3.102',{
                    pd_no:productInfo.pd_no,
                    mark_type:10,
                    relation_field:session.getItem('time_stamp'),
                    risk_level:productInfo.risk_level,
                    risk_accept:user.invester_class == 2 ? -1 : user.risk_accept  //专业投资者置成-1
                }).success(function(){
                    if(_this.isApprop){
                        //进入风险讲解页面
                        LDFW.navigate('product/risk_explain/'+_this.pd_no)
                    }else{
                        //如果客户与产品不匹配，需要签署不适当警示书
                        LDFW.navigate('product/warn_confirm/'+_this.pd_no)
                    }
                })
            },
            disableContinuePurchase:function(flag){
                if(flag){
                    this.$el.find('[operate="continue"]').addClass('weui-btn_disabled').attr('disabled','disabled')
                }else{
                    this.$el.find('[operate="continue"]').removeClass('weui-btn_disabled').removeAttr('disabled')
                }
            },
            analysisAppropResult:function(product){
                let user = session.getCurrentUser()
                let appropResult = {
                    risk:{
                        result:false,
                        risk_level:dict.getDict('risk_level',product.risk_level),
                        risk_accept: dict.getDict('risk_accept',user.risk_accept),
                        risk_accept_name:user.cust_type===1?
                            dict.getDict('risk_accept_co',user.risk_accept):dict.getDict('risk_accept_cust',user.risk_accept),
                        low_risk_flag:false,
                        professional_invester:false
                    },
                    type:false,
                    period:false
                }
                let riskNotification = {
                    isApprop:true,
                    pd_name:product.pd_name,
                    pd_code:product.pd_code
                }
                if(product.risk_level >1 && user.low_risk_flag == 1){
                    riskNotification.isApprop = false
                    appropResult.risk.low_risk_flag = true
                    this.$el.find('[operate="continue"]').text('最低风险 无法购买').css({'font-size':'13px'})
                }
                if(product.risk_level <= user.risk_accept || user.invester_class == 2){
                    appropResult.risk.result = true
                    appropResult.risk.professional_invester = true
                    riskNotification.isApprop = true
                }else{
                    riskNotification.isApprop = false
                }
                //dead code
                if(true){//用来判断产品类型和客户的投资偏好
                    appropResult.type = true
                }else{
                    riskNotification.isApprop = false
                }
                if(true){//用来判断产品期限和客户的投资期限偏好
                    appropResult.period = true
                }else{
                    riskNotification.isApprop = false
                }
                this.$el.find('#approp_result').html(art.compile(resultTpl)(appropResult))
                this.$el.find('#risk_notification').html(art.compile(notificationTpl)(riskNotification))
                this.isApprop = riskNotification.isApprop
                this.low_risk_flag = appropResult.risk.low_risk_flag
            },
            cancelPurchase:function(event){
                LDFW.navigate('product/detail/'+this.pd_no)
            },
            acceptConfirm:function(event){
                if(event.target.checked && !this.low_risk_flag){
                    this.disableContinuePurchase(false)
                }else{
                    this.disableContinuePurchase(true)
                }
            }
        })
        return new View()
    })